<template>
  <div>
    <div>
      <!-- <h3 v-show="!editDate">邮件通知设置：</h3> -->
      <el-form ref="form" :model="form">
        <el-form-item label="发送邮件域名：" required>
          <el-input
            v-model.trim="form.configValue['spring.mail.host']"
            clearable
          />
        </el-form-item>
        <el-form-item label="发送邮件邮箱：" required>
          <el-input
            v-model.trim="form.configValue['spring.mail.username']"
            clearable
          />
        </el-form-item>
        <el-form-item label="发送邮件邮箱密码：" required>
          <el-input
            v-model.trim="form.configValue['spring.mail.password']"
            clearable
          />
        </el-form-item>
        <div class="classCenter">
          <el-button class="filter-item" type="primary" @click="handleSave"
            >保存</el-button
          >
          <el-button
            class="filter-item"
            style="margin-left: 10px"
            type="primary"
            v-show="editDate"
            @click="handlecancle(false)"
            >取消</el-button
          >
        </div>
      </el-form>
    </div>
  </div>
</template>

<script>
import { jlcpCodeConfigSave, jlcpCodeConfigUpdate } from "@/api/lowcode";
export default {
  name: "emailSettings",
  components: {},
  props: ["editDate"],
  data() {
    return {
      form: {
        projectId: +sessionStorage.getItem("developLowCodeId"),
        configItem: "emailConfig",
        configDesc: "邮件通知设置",
        configValue: {
          "spring.mail.host": "",
          "spring.mail.username": "",
          "spring.mail.password": "",
        },
        configType: "EMAIL",
      },
    };
  },
  // created() {
  //   if (this.editDate) {
  //     this.form = JSON.parse(JSON.stringify(this.editDate));
  //   }
  // },
  //由于该组件添加了keep-alive缓存，所以每次进入这个组件调用activated生命周期
  activated() {
    if (this.editDate) {
      this.form = this.editDate;
    }
  },
  methods: {
    handlecancle(flag) {
      if (this.editDate) {
        this.$emit("close", flag);
      } else {
        this.$parent.$parent.$parent.handledTableBtnSave(
          "邮件通知设置",
          "通知配置列表",
          "notificationListComponents"
        );
      }
    },
    handleSave() {
      this.$refs["form"].validate(async (valid) => {
        if (valid) {
          if (
            !(
              this.form.configValue["spring.mail.host"] &&
              this.form.configValue["spring.mail.username"] &&
              this.form.configValue["spring.mail.password"]
            )
          ) {
            return this.$message({
              message: `输入项不能为空`,
              type: "warning",
              showClose: true,
            });
          }
          let res = null;
          const parmes = JSON.parse(JSON.stringify(this.form));
          parmes.configValue = JSON.stringify(parmes.configValue);
          if (this.editDate) {
            res = await jlcpCodeConfigUpdate(parmes);
          } else {
            res = await jlcpCodeConfigSave(parmes);
          }
          if (res?.code === 200) {
            this.$message({
              message: "操作成功",
              type: "success",
            });
            this.handlecancle(true);
          }
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
@import "~@/styles/layout-table.scss";
$rowLength: 2;
@media all and (min-width: 1659px) {
  $rowLength: 2;
  @include calculation($rowLength: $rowLength);
}
@media all and (max-width: 1659px) {
  $rowLength: 3;
  @include calculation($rowLength: $rowLength);
}
.titleClass {
  margin-bottom: 10px;
  color: #5a5e66;
  font-weight: 700;
}
.bigbox {
  border: 1px solid #ccc;
  padding-top: 15px;
}
.classCenter {
  display: flex;
  justify-content: center;
}
.commonClass {
  display: flex;
  justify-content: space-between;
  padding: 0 20px;
}
</style>
